package main

import (
	"common"
	"common/configx"
	"common/databasex"
	"common/zapx"
	"flag"
	"go.uber.org/zap"
	"gorm.io/gen"
)

var configPath = flag.String("config_path", "./config.yaml", "配置文件位置")

func main() {
	flag.Parse()
	if err := common.Init(
		*configPath,
		common.WithBootstrapInit(func(config configx.Config) any {
			return zapx.WithAppName(config.App.Name)
		}),
		common.WithBootstrapInit(func(config configx.Config) any {
			return databasex.WithMysqlDns(config.Mysql.Dns)
		}),
		common.WithBootstrapInit(func(config configx.Config) any { return zapx.WithDefaultLogger() }),
	); err != nil {
		panic(err)
	}

	bt := common.NewBootstrap()
	if err := bt.Boot(); err != nil {
		zapx.Logger.Error("应到程序启动失败", zap.Error(err))
		return
	}
	defer func() {
		bt.Shutdown()
	}()
	g := gen.NewGenerator(gen.Config{
		OutPath: "../models/dal/query",
		Mode:    gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
	})

	g.UseDB(databasex.M.DB)

	g.ApplyBasic(g.GenerateAllTable()...)

	// 执行并生成代码
	g.Execute()
}
